<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-form-container">
        <h3 class="title">后台管理系统</h3>
        <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" placeholder="用户名">
              <template #prefix>
                <i class="el-icon-user"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码" @keyup.enter="handleLogin">
              <template #prefix>
                <i class="el-icon-lock"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%;" @click.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { loginApi } from '@/api'
import { setToken } from '@/utils/auth'
import { asyncRoutes, constantRoutes } from '@/router'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          // 由于没有实际后端，这里直接模拟登录成功
          // 设置token，否则路由守卫会重定向回登录页
          setToken('admin-token')
          
          // 先生成动态路由，再进行重定向
          const roles = ['admin']; // 默认角色
          this.$store.dispatch('permission/generateRoutes', {
            roles,
            routes: asyncRoutes,  // 使用router/index.js中定义的asyncRoutes
            constantRoutes: constantRoutes  // 使用router/index.js中定义的constantRoutes
          }).then(accessRoutes => {
            // 动态添加可访问路由
            this.$router.addRoutes(accessRoutes)
            
            this.loading = false
            
            // 跳转到重定向的路由或首页
            const redirectPath = this.redirect || '/dashboard'
            
            // 使用replace模式，避免导航问题
            this.$router.replace(redirectPath).catch(err => {
              console.error('导航错误:', err)
              // 如果导航失败，尝试跳转到首页
              this.$router.replace('/')
            })
            
            this.$messageBox.success('登录成功')
          })
          
          /* 实际项目中应使用API调用
          loginApi(this.loginForm).then(response => {
            this.loading = false
            // 保存token
            setToken(response.token)
            const redirectPath = this.redirect || '/dashboard'
            this.$router.push(redirectPath)
          }).catch(error => {
            this.loading = false
            this.$message.error(error.message || '登录失败')
          })
          */
        } else {
          console.log('表单验证失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}
</style>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0;
  overflow: hidden;
  
  .login-content {
    width: 100%;
    display: flex;
    justify-content: center;
    
    .login-form-container {
      width: 400px;
      padding: 40px 35px;
      border-radius: 5px;
      background: #fff;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      
      .title {
        text-align: center;
        margin-bottom: 30px;
        font-size: 26px;
        font-weight: bold;
        color: #409EFF;
      }
      
      .el-form-item {
        margin-bottom: 25px;
      }
    }
    
    @media screen and (max-width: 768px) {
      .login-form-container {
        width: 90%;
        padding: 30px 20px;
      }
    }
  }
}
</style> 